package com.sinven.controller.manage;

import com.sinven.common.ActionResult;
import com.sinven.pojo.Teacher;
import com.sinven.service.TeacherService;
import com.sinven.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/manage")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @GetMapping("teachers")
    public ActionResult<List<Teacher>> queryTeachers(@RequestParam(value = "curPage", defaultValue = "1") Integer curPage,
                                                     @RequestParam(value = "pageSize", defaultValue = "12") Integer pageSize) {
        return teacherService.queryCurPageData(curPage, pageSize);
    }

    @GetMapping("teachers/{tid}")
    public ActionResult<Teacher> getTeacher(@PathVariable("tid") long tid) {
        return ResultUtil.success(teacherService.getTeacherByTid(tid));
    }
    @PostMapping("teachers")
    public ActionResult addTeacher(Teacher teacher) {
        boolean result = teacherService.addTeacher(teacher);
        if (result) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(500, "服务器错误");
        }
    }

    @PutMapping("teachers")
    public ActionResult updateTeacher(Teacher teacher) {
        boolean result = teacherService.updateTeacher(teacher);
        if (result) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(500, "服务器错误");
        }
    }

    @DeleteMapping("teachers")
    public ActionResult deleteTeacher(long tid){
        boolean result = teacherService.deleteTeacher(tid);
        if (result) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(500, "服务器错误");
        }
    }
}
